package com.css.sword.knowing.search.tool;

import java.util.HashMap;
import java.util.Map;

import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import com.css.sword.knowing.search.Searcher;

/**
 * 索引mapping工具类，操作mapping
 * 
 * @author 作者 E-mail:caihx@css.com.cn
 * @version 创建时间：2016年11月1日 上午9:23:01 类说明
 */
public class MappingTool {

    /**
     * 创建mapping，可以理解为数据库的表结构
     * 
     * @param indexName
     *            索引名称
     * @param typeName
     *            类型名称
     * @param builder
     *            已经对象化的 json格式的mapping数据
     * @param settings
     *            索引的setting属性设置
     * @return true：成功，false：失败
     */
    public static boolean create(String indexName, String typeName, XContentBuilder builder,
            Map<String, Object> settings) {
        return create(null, indexName, typeName, builder, settings);
    }
    
    public static boolean create(TransportClient client, String indexName, String typeName, XContentBuilder builder,
            Map<String, Object> settings) {
        boolean ret = false;

        if (indexName == null || typeName == null || indexName.isEmpty() || typeName.isEmpty())
            return ret;
        
        if(null == client)
        	client = Searcher.getTransportClient();

        if (!IndexTool.indexExists(client, indexName))
            if (!IndexTool.create(client, indexName, settings))
                return ret;

        PutMappingRequest req = Requests.putMappingRequest(indexName).type(typeName).source(builder);
        PutMappingResponse res = client.admin().indices().putMapping(req).actionGet();

        if (res != null)
            ret = res.isAcknowledged();
        return ret;
    }
    
    
    /**
     * 创建mapping，可以理解为数据库的表结构
     * 
     * @param indexName
     *            索引名称
     * @param typeName
     *            类型名称
     * @param fieldList
     *            Map格式的字段数据
     * @param settings
     *            索引的setting属性设置
     * @return true：成功，false：失败
     */
    public static boolean create(String indexName, String typeName, Map<String, Object> fieldList,
            Map<String, Object> settings) {
        return create(null, indexName, typeName, fieldList, settings);
    }
    
    public static boolean create(TransportClient client, String indexName, String typeName, Map<String, Object> fieldList,
            Map<String, Object> settings) {
        boolean ret = false;

        if (indexName == null || typeName == null || indexName.isEmpty() || typeName.isEmpty())
            return ret;

        if(null == client)
        	client = Searcher.getTransportClient();
        
        Map<String, Object> properties = new HashMap<String, Object>();
		properties.put("properties", fieldList);
		
		Map<String, Object> mapping = new HashMap<String, Object>();
		mapping.put(typeName, properties);
        
        if (!IndexTool.indexExists(client, indexName))
            if (!IndexTool.create(client, indexName, settings))
                return ret;

        PutMappingRequest req = Requests.putMappingRequest(indexName).type(typeName).source(mapping);
        PutMappingResponse res = client.admin().indices().putMapping(req).actionGet();

        if (res != null)
            ret = res.isAcknowledged();
        return ret;
    }

    /**
     * 删除mapping
     * 
     * @param indexName
     *            索引名称
     * @param typeNames
     *            索引类型数组
     * @return true：成功，false：失败
     */
    public static boolean drop(String indexName, String[] typeNames) {
      
        return drop(null, indexName, typeNames);
    }
    
    public static boolean drop(TransportClient client, String indexName, String[] typeNames) {
        boolean ret = false;

//        if (null== indexName || indexName.isEmpty() || null == typeNames || typeNames.length == 0)
//            return ret;
//        
//        if(null == client)
//        	client = Searcher.getTransportClient();
//
//        if (IndexTool.indexExists(client, indexName)) {
//
//        	for(int i = 0; i<typeNames.length; i++)
//        	{
//        		GetMappingsRequest getReq = new GetMappingsRequest();
//        		getReq.indices(indexName);
//        		getReq.types(typeNames[i]);
//        		GetMappingsResponse getRes = client.admin().indices().getMappings(getReq).actionGet();
//                if (null == getRes || getRes.getMappings().size() == 0)
//                    return ret;
//        	}
//        	
//            DeleteMappingRequest req = Requests.deleteMappingRequest(indexName);
//            
//            req.types(typeNames);
//
//            DeleteMappingResponse res = client.admin().indices().deleteMapping(req).actionGet();
//            if (res != null)
//                ret = res.isAcknowledged();
//        }

        return ret;
    }
    
    /**
     * 判断索引类型名称是否存在
     * 
     * @param indexName
     *            索引名称
     * @param typeName
     *            索引类型
     * @return true：存在，false：不存在
     */
    public static boolean typeNameExist(String indexName, String typeName) {
    
        return typeNameExist(null, indexName, typeName);
    }
    
    public static boolean typeNameExist(TransportClient client, String indexName, String typeName) {
        boolean ret = false;

         if (null== indexName || indexName.isEmpty() || null == typeName || typeName.isEmpty())
             return ret;

         if(null == client)
         	client = Searcher.getTransportClient();
         
         if(IndexTool.indexExists(client, indexName))
         {
            GetMappingsRequest req = new GetMappingsRequest();
    
            req.indices(indexName);
            req.types(typeName);
    
            GetMappingsResponse res = client.admin().indices().getMappings(req).actionGet();
            if (res != null)
                if(res.getMappings().size() > 0)
                    ret = true;
         }
    
        return ret;
    }
}
